package com.ydgk.servlet;

import com.ydgk.bean.Book;
import com.ydgk.dao.BookDao;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;

@WebServlet(name = "BookUpdateByIdServlet", urlPatterns = "/BookUpdateByIdServlet.action")
public class BookUpdateByIdServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Book book = new Book();
        BookDao bookDao = new BookDao();
        String pageNumber = request.getParameter("pageNumber");
        try {
            BeanUtils.populate(book,request.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        int i = bookDao.updateById(book);
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        if(i>0){
            RequestDispatcher rd = request.getRequestDispatcher("BookPagingServlet.action?pageNumber="+pageNumber);
            rd.forward(request,response);
        }else {
            out.print("修改失败！");
        }
    }


}
